<template>
    <image class="img" :src="qrCode" @click="previewIimage"/>
    <view class="tips">分享二维码,获取专属福利</view>
</template>

<script setup lang="ts">
import { getQrcode } from '@/api/index'
import { ref } from 'vue'
import { useMemberStore } from '@/stores'
import { storeToRefs } from 'pinia'
import { onLoad } from '@dcloudio/uni-app'
const { memberInfo }  = storeToRefs(useMemberStore())

onLoad(() => {
    getCode()
})

const qrCode = ref("")
// 获取二维码
const getCode = async () => {
    const code = uni.getStorageSync("qrcode")
    if(code){
        qrCode.value = code
        return
    }
    let par = {
        path:"pages/index/index",
        scene:memberInfo.value.id + "&2",
        width:200
    }
    
    const res:any = await getQrcode(par)
    if(res.code == 0 && res.data){
        let qrcodeimg = `data:image/png;base64,${res.data}`;
        qrCode.value = qrcodeimg
        uni.setStorageSync("qrcode", qrcodeimg)
    }
}
const previewIimage = () =>{
   uni.previewImage({
     urls: [qrCode.value]
   })
}

</script>

<style lang="scss" scoped>
.img{
   display: block;
   width: 500rpx;
   height: 500rpx;
   margin: 24rpx auto;
   margin-top: 200rpx;
}
.tips{
   width: 100%;
   padding: 0 48rpx;
   text-align: center;
   box-sizing: border-box;
   margin-top: 48rpx;
}

</style>